package com.hxkj.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxkj.dao.LabelExtDao;
import com.hxkj.dao.LabelManagementDao;
import com.hxkj.dao.LabelUsageInfoDao;
import com.hxkj.domain.po.LabelExt;
import com.hxkj.domain.po.LabelManagement;
import com.hxkj.domain.po.LabelUsageInfo;
import com.hxkj.domain.vo.LabelExtVO;
import com.hxkj.domain.vo.LabelManagementVO;
import com.hxkj.service.LabelConfService;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.domain.to.PageTo;
import javafx.scene.control.Labeled;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class LabelConfServiceImpl implements LabelConfService {

    private static final Logger LOGGER = LoggerFactory.getLogger(LabelConfServiceImpl.class);

    @Autowired
    private LabelManagementDao labelManagementDao;
    @Autowired
    private LabelExtDao labelExtDao;
    @Autowired
    private LabelUsageInfoDao labelUsageInfoDao;

    @Override
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public ResultMessageStruct save(Map<String, Object> map) throws Exception{
        ResultMessageStruct result = new ResultMessageStruct();
        JSONObject data = (JSONObject) map.get("data");
        LabelManagement labelManagement = JSONObject.toJavaObject(data, LabelManagement.class);
        JSONArray exts = map.get("extList")==null?null:(JSONArray)map.get("extList");
        //JSONArray usages = map.get("usageList")==null?null:(JSONArray)map.get("usageList");

        List<LabelManagement> list = labelManagementDao.get(labelManagement);
        if(list != null && list.size()>0){
            result.setCode(88);
            result.setMessage("数据已经存在，不能重复添加！");
            return result;
        }

        int i = 0;
        i = labelManagementDao.save(labelManagement);
        List<LabelExt> extList = new ArrayList<>();
        //List<LabelUsageInfo> usageList = new ArrayList<>();
        if(exts != null){
            for (Object obj : exts) {
                JSONObject json = (JSONObject) obj;
                LabelExt labelExt = JSONObject.toJavaObject(json, LabelExt.class);
                labelExt.setLabelId(labelManagement.getLabelId());
                labelExt.setCreateor(labelManagement.getCreateor());
                labelExt.setCreateTime(System.currentTimeMillis());
                labelExt.setModifyBy(labelManagement.getCreateor());
                labelExt.setUpdateTime(System.currentTimeMillis());
                extList.add(labelExt);
            }
            i = labelExtDao.batchInsertExt(extList);
        }
//        if(usageList != null){
//            for (Object obj : usages) {
//                JSONObject json = (JSONObject) obj;
//                LabelUsageInfo labelUsageInfo = JSONObject.toJavaObject(json, LabelUsageInfo.class);
//                labelUsageInfo.setLabelId(labelManagement.getLabelId());
//                labelUsageInfo.setCreateor(labelManagement.getCreateor());
//                labelUsageInfo.setCreateTime(System.currentTimeMillis());
//                usageList.add(labelUsageInfo);
//            }
//            i = labelUsageInfoDao.batchInsertUsage(usageList);
//        }

        if(i > 0){
            result.setCode(200);
            result.setMessage("保存成功！");
        }else{
            result.setCode(0);
            result.setMessage("保存失败！");
        }

        return result;
    }

    @Override
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public ResultMessageStruct saveLabel(LabelManagement labelManagement) throws Exception {
        ResultMessageStruct result = new ResultMessageStruct();

        List<LabelManagement> list = labelManagementDao.get(labelManagement);
        if(list != null && list.size()>0){
            result.setCode(88);
            result.setMessage("数据已经存在，不能重复添加！");
            return result;
        }

        int i = labelManagementDao.save(labelManagement);
        if(i == 1){
            result.setCode(i);
            result.setMessage("保存成功！");
        }else{
            result.setCode(0);
            result.setMessage("保存失败！");
        }
        return result;
    }

    @Override
    public ResultDataStruct<LabelManagement> get(LabelManagement labelManagement) throws Exception {
        ResultDataStruct<LabelManagement> result = new ResultDataStruct<>();
        Long labelId = labelManagement.getLabelId();
        LabelManagement labelManagement1 = labelManagementDao.get(labelId);
        LabelExt labelExt = new LabelExt();
        labelExt.setLabelId(labelId);
        List<LabelExt> extList = labelExtDao.get(labelExt);
//        LabelUsageInfo labelUsageInfo = new LabelUsageInfo();
//        labelUsageInfo.setLabelId(labelId);
//        List<LabelUsageInfo> usageInfoList = labelUsageInfoDao.get(labelUsageInfo);

        labelManagement1.setExtlist(extList);
//        labelManagement1.setUsageInfoList(usageInfoList);
        result.setData(labelManagement1);
        result.setCode(200);
        return result;
    }

    @Override
    public PageInfo<LabelManagementVO> getLabelList(LabelManagement labelManagement, PageTo pageParam) throws Exception {
        Page<LabelManagementVO> page = PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        labelManagementDao.getList(labelManagement);
        PageInfo<LabelManagementVO> pageInfo = new PageInfo<>(page);
        for (LabelManagementVO labelManagementVO : pageInfo.getList()) {
            LabelExt entity = new LabelExt();
            entity.setLabelId(labelManagementVO.getLabelId());
            List<LabelExt> extList = labelExtDao.get(entity);

            List<LabelExtVO> evolist = new ArrayList<>();
            if(extList != null && extList.size()>0){
                for (LabelExt labelExt : extList) {
                    LabelExtVO evo = new LabelExtVO();
                    evo.setAllowValue(labelExt.getAllowValue());
                    evo.setDataType(labelExt.getDataType());
                    evo.setLabelExtId(labelExt.getLabelExtId());
                    evo.setLabelId(labelExt.getLabelId());
                    evo.setName(labelExt.getName());
                    evo.setUsageDesc(labelExt.getUsageDesc());
                    evolist.add(evo);
                }
                labelManagementVO.setExtlist(evolist);
            }
        }
        return pageInfo;
    }

    @Override
    public ResultMessageStruct modify(Map<String, Object> map) throws Exception{
        ResultMessageStruct result = new ResultMessageStruct();
        JSONObject data = (JSONObject) map.get("data");
        LabelManagement labelManagement = JSONObject.toJavaObject(data, LabelManagement.class);
        JSONArray exts = map.get("extList")==null?null:(JSONArray)map.get("extList");
        List<LabelExt> extList = new ArrayList<>();
        try{
            int i = labelManagementDao.update(labelManagement);
            LabelExt entity = new LabelExt();
            entity.setLabelId(labelManagement.getLabelId());
            i = labelExtDao.delete(entity);

            if(exts != null){
                for (Object obj : exts) {
                    JSONObject json = (JSONObject) obj;
                    LabelExt labelExt = JSONObject.toJavaObject(json, LabelExt.class);
                    labelExt.setLabelId(labelManagement.getLabelId());
                    labelExt.setCreateor(labelManagement.getCreateor());
                    labelExt.setCreateTime(System.currentTimeMillis());
                    extList.add(labelExt);
                }
                i = labelExtDao.batchInsertExt(extList);
            }
            if(i==0){
                result.setCode(0);
                result.setMessage("修改数据失败!");
                return result;
            }
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(603);
            result.setMessage("修改数据失败!");
            return result;
        }
        result.setCode(200);
        result.setMessage("修改成功！");
        return result;
    }

    @Override
    public ResultMessageStruct remove(String labelIds) throws Exception {
        ResultMessageStruct result = new ResultMessageStruct();
        String[] ids = labelIds.split(",");
        Map<String, Object> map = new HashMap<>();
        map.put("array",ids);
        int i = labelManagementDao.deleteLabelManagement(map);
        if(i > 0){
            result.setCode(200);
            result.setMessage("删除成功！");
        }else{
            result.setCode(0);
            result.setMessage("删除失败！");
        }
        return result;
    }


    @Override
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public ResultMessageStruct editLabelPlus(Map<String, Object> map) throws Exception{
        ResultMessageStruct result = new ResultMessageStruct();
        int i = 0;
        String type = map.get("type").toString();
        String operate = map.get("operate").toString();
        JSONArray jsonArray = map.get("data")==null?null:(JSONArray)map.get("data");
        if("ext".equals(type)){
            if("save".equals(operate)){
                List<LabelExt> list = new ArrayList<>();
                for (Object o : jsonArray) {
                    JSONObject obj = (JSONObject) o;
                    LabelExt labelExt = JSONObject.toJavaObject(obj, LabelExt.class);
                    list.add(labelExt);
                }
                i = labelExtDao.batchInsertExt(list);
            }else{
                for (Object o : jsonArray) {
                    JSONObject obj = (JSONObject) o;
                    LabelExt labelExt = JSONObject.toJavaObject(obj, LabelExt.class);
                    i = labelExtDao.update(labelExt);
                }
            }
        }else{
            if("save".equals(operate)){
                List<LabelUsageInfo> list = new ArrayList<>();
                for (Object o : jsonArray) {
                    JSONObject obj = (JSONObject) o;
                    LabelUsageInfo labelUsageInfo = JSONObject.toJavaObject(obj, LabelUsageInfo.class);
                    list.add(labelUsageInfo);
                }
                i = labelUsageInfoDao.batchInsertUsage(list);
            }else{
                for (Object o : jsonArray) {
                    JSONObject obj = (JSONObject) o;
                    LabelUsageInfo labelUsageInfo = JSONObject.toJavaObject(obj, LabelUsageInfo.class);
                    i = labelUsageInfoDao.update(labelUsageInfo);
                }
            }
        }
        if(i > 0){
            result.setCode(i);
            result.setMessage("操作数据成功！");
        }else{
            result.setCode(0);
            result.setMessage("操作数据失败！");
        }
        return result;
    }

    /************************标签使用情况表相关业务****************************/

    @Override
    public ResultMessageStruct saveLabelExt(LabelExt labelExt) throws Exception {
        ResultMessageStruct result = new ResultMessageStruct();

        List<LabelExt> list = labelExtDao.get(labelExt);
        if(list != null && list.size()>0){
            result.setCode(344);
            result.setMessage("数据已经存在，请不要重复保存");
            return result;
        }

        int i = labelExtDao.save(labelExt);
        if(i == 1){
            result.setCode(i);
            result.setMessage("保存成功！");
        }else{
            result.setCode(0);
            result.setMessage("保存失败！");
        }
        return result;
    }

    @Override
    public ResultDataStruct<List<LabelExt>> getLabelExt(LabelExt labelExt) throws Exception {
        ResultDataStruct<List<LabelExt>> result = new ResultDataStruct<>();
        List<LabelExt> list = labelExtDao.get(labelExt);
        result.setData(list);
        return result;
    }

    @Override
    public ResultMessageStruct modifyLabelExt(LabelExt labelExt) throws Exception {
        ResultMessageStruct result = new ResultMessageStruct();
        try{
            int i = labelExtDao.update(labelExt);
            if(i==0){
                result.setCode(0);
                result.setMessage("修改数据失败!");
                return result;
            }
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(403);
            result.setMessage("修改数据失败!");
            return result;
        }
        result.setMessage("修改成功！");
        return result;
    }

    @Override
    public ResultMessageStruct removeLabelExt(LabelExt labelExt) throws Exception {
        ResultMessageStruct result = new ResultMessageStruct();
        int i = labelExtDao.delete(labelExt.getLabelExtId());
        if(i == 1){
            result.setCode(i);
            result.setMessage("删除成功！");
        }else{
            result.setCode(0);
            result.setMessage("删除失败！");
        }
        return result;
    }

    /************************标签使用情况表相关业务****************************/

    @Override
    public ResultMessageStruct saveLabelUsage(LabelUsageInfo labelUsageInfo) throws Exception {
        ResultMessageStruct result = new ResultMessageStruct();

        List<LabelUsageInfo> list = labelUsageInfoDao.get(labelUsageInfo);
        if(list != null && list.size()>0){
            result.setCode(345);
            result.setMessage("数据已经存在，请不要重复保存");
            return result;
        }

        int i = labelUsageInfoDao.save(labelUsageInfo);
        if(i == 1){
            result.setCode(i);
            result.setMessage("保存成功！");
        }else{
            result.setCode(0);
            result.setMessage("保存失败！");
        }
        return result;
    }

    @Override
    public ResultDataStruct<List<LabelUsageInfo>> getLabelUsage(LabelUsageInfo labelUsageInfo) throws Exception {
        ResultDataStruct<List<LabelUsageInfo>> result = new ResultDataStruct<>();
        List<LabelUsageInfo> list = labelUsageInfoDao.get(labelUsageInfo);
        result.setData(list);
        return result;
    }

    @Override
    public ResultMessageStruct modifyLabelUsage(LabelUsageInfo labelUsage) throws Exception {
        ResultMessageStruct result = new ResultMessageStruct();
        try{
            int i = labelUsageInfoDao.update(labelUsage);
            if(i==0){
                result.setCode(0);
                result.setMessage("修改数据失败!");
                return result;
            }
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(403);
            result.setMessage("修改数据失败!");
            return result;
        }
        result.setMessage("修改成功！");
        return result;
    }

    @Override
    public ResultMessageStruct removeLabelUsage(LabelUsageInfo labelUsage) throws Exception {
        ResultMessageStruct result = new ResultMessageStruct();
        int i = labelUsageInfoDao.delete(labelUsage.getLabelUsageId());
        if(i == 1){
            result.setCode(i);
            result.setMessage("删除成功！");
        }else{
            result.setCode(0);
            result.setMessage("删除失败！");
        }
        return result;
    }
}
